import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import goodTypeApi from '@/api/goodTypeApi';

export const useGoodTypeStore = defineStore('goodType', () => {
	// 定义状态
	const goodTypeTree = ref([]);
	const topIdx = ref(0)
	const leftList = computed(() => goodTypeTree.value[topIdx.value].children);
	const leftIdx = ref(0);

	// 定义函数

	const HGetGoodTypeTree = () => {
		goodTypeApi
			.tree()
			.then((res) => {
				goodTypeTree.value = res.data.map((val1) => {
					
					// val1.icon = val1.img[0].url
					
					// if(val1.children) {
					// 	val1.children.map(val2 => {
					// 		val2.icon = val2.img[0].url
					// 		return val2
					// 	})
					// }
					return val1
				})

				console.log('goodTypeTree.value', goodTypeTree.value);
			})
			.catch((err) => {
				console.error(err); // 处理错误情况，例如显示错误提示信息。
			});
	};

	const HTopChange = (id) => {
		topIdx.value = goodTypeTree.value.findIndex((v) => v.id == id);
	}

	// 返回状态和函数，以便在组件中使用
	return {
		goodTypeTree,
		topIdx,
		leftList,
		leftIdx,
		HGetGoodTypeTree,
		HTopChange
	};
});